package Dao.Impl;

import Dao.NoticeDao;
import Domain.Notice;
import JDBCUtils.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class NoticeDaoImpl implements NoticeDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<Notice> FindAllNotice() {
        String sql ="select\n" +
                " t.id  ,t.title, t.msg , t.pubtime , t1.username , t.adminid\n" +
                "from\n" +
                " notice t\n" +
                "join\n" +
                " admin t1\n" +
                "on\n" +
                " t.adminid = t1.id\n" +
                "where\n" +
                " 1=1";
        List<Notice> result = null;
        try {
            result = template.query(sql,new BeanPropertyRowMapper<>(Notice.class));
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public List<Notice> FindNoticeById(int id) {
        String sql ="select\n" +
                " t.id , t.title , t.msg , t.pubtime , t1.username , t.adminid\n" +
                "from\n" +
                " notice t\n" +
                "join\n" +
                " admin t1\n" +
                "on\n" +
                " t.adminid = t1.id\n" +
                "where\n" +
                " t.id=?";
        List<Notice> result = null;
        result = template.query(sql,new BeanPropertyRowMapper<>(Notice.class),id);
        return result;
    }
}
